package cn.bdqn.controller;

import cn.bdqn.pojo.SysUser;
import cn.bdqn.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
    @Resource
    SysUserService sysUserService;
    @RequestMapping(value="/login",method= RequestMethod.POST)
    public String login(String usrName, String usrPassword,
                        Model model, HttpSession session){
            //调用service方法，进行用户匹配
        SysUser sysUser = sysUserService.login(usrName, usrPassword);
        if(sysUser != null){//登录成功
            //放入session
            session.setAttribute("loginUser", sysUser);
            //返回逻辑视图进行页面跳转（main.jsp）
            return "redirect:/main";
        }else{
            //页面跳转（login.jsp）带出提示信息--转发
            model.addAttribute("msg", "用户名或密码不正确");
            //转发到login.jsp
            return "forward:/login.jsp";
        }
    }
    @RequestMapping("/main")
    public String main(){
        return "main";
    }
}
